*****************************************************************
*********** Mining and violent competition in Africa ************
****** A.S. Rigterink, T. Ghani, J.S. Lozano, J.N. Shapiro ******
************ Replication of Table B3 of the Appendix ************

/*
0. Setting globals
1. Preparing IPIS data
2. Linking IPIS data to ML commodity predictions
3. Determining modal ASM commodity
4. Comparing IPIS and ASM predictions
5. Creating Table 


*/
*****************************************************************

clear all
version 17.0
set more off
cap log close

/*--------------------------------
------- 0. Setting globals -------
----------------------------------*/

*** Directory globals
*** NB: SET YOUR DIRECTORY HERE AND UNTEXT IF NOT RUNNING 0-Master.do
*global dir = ".\Replication_data\Stata"
*cd "$dir"

*** Subdirectories
global Do_files     "./Do"
global Data			"./Data"
global Results		"./Results"
global Tables 		"./Tables"

*** Scripts for spatially clustered standard errors
do "$Do_files/my_ols_spatial_HAC.do"
do "$Do_files/my_reg2hdfespatial.ado"

/*-------------------------------------
------- 1. Preparing IPIS data  -------
---------------------------------------*/

use "$Data/ipis_gridded.dta"

* Determining modal mineral 
keep if mineral=="aluminum" | mineral=="coal" | mineral=="cobalt" | mineral=="coltan" | mineral=="copper" | mineral=="diamond" | mineral=="gold" | mineral=="iron" | mineral=="lead" | mineral=="manganese" | mineral=="nickel" | mineral=="phosphate" | mineral=="platinum" | mineral=="tin" | mineral=="tungsten" | mineral=="silver" | mineral=="zinc"
drop if gid==.

gen count = 1
collapse (rawsum) count, by(gid mineral)

* Reshaping again
bysort gid: gen nominerals = _n
reshape wide mineral count, i(gid) j(nominerals)

* Continuing to determine modal mineral 
** NB: dealing with cells with equal numbers of deposits by allocating at random
gen modal_mineral_asm = "" 

egen maxcount = rowmax(count?)

gen nomodal = 1 if /// 
(maxcount==count1 & maxcount==count2) | (maxcount==count1 & maxcount==count3) | (maxcount==count2 & maxcount==count3)

forvalues i = 1(1)5 {
	replace modal_mineral_asm = mineral`i' if maxcount==count`i' & nomodal!=1
}


drop count? maxcount

/*---------------------------------------------------------------
------- 2. Linking IPIS data to ML commodity predictions  -------
-----------------------------------------------------------------*/

merge 1:m gid using "$Data/main.dta", keepusing(snl_* asm_voted asm_voted_*  year gid_0 owner_oecd9 owner_isin owner_tax owner_chn owner_local) nogen

keep if year==2000

* All duplicates are border cells
duplicates drop gid snl_commodity, force

/*-------------------------------------------------
------- 3. Determining modal ASM commodity  -------
---------------------------------------------------*/

egen maxprob =  rowmax(asm_voted_prob_2c asm_voted_prob_3t asm_voted_prob_diamond asm_voted_prob_gold)
gen modal_asm = ""


foreach X in 2c 3t diamond gold {
    
	replace modal_asm = "`X'" if maxprob==asm_voted_prob_`X' & asm_voted==1
	
}

egen temp = rowmax(asm_voted_gold asm_voted_diamond asm_voted_2c asm_voted_3t)
replace asm_voted = temp
drop temp

gen snl_commodity1 = snl_commodity
replace snl_commodity1 = "2c" if snl_commodity=="copper" | snl_commodity=="cobalt"
replace snl_commodity1 = "3t" if snl_commodity=="coltan" | snl_commodity=="tin" | snl_commodity=="tungsten"

gen snl_2c = 1 if snl_commodity1=="2c"
gen snl_3t = 1 if snl_commodity1=="3t"
for var snl_2c snl_3t: replace X=0 if X!=.

/*----------------------------------------------------
------- 4. Comparing IPIS and ASM predictions  -------
------------------------------------------------------*/

*** Degree of overlap ***
tab snl_dummy asm_voted if snl_commodity!="none"
sum owner_isin if snl_commodity!="none" & asm_voted!=. 
sum owner_* if owner_isin==1 & snl_commodity!="none" & asm_voted!=. 

*** Modal mineral in SNL versus ASM, real ASM ***
gen same_modal_snl_asm = 1 if snl_commodity==modal_mineral_asm 
replace same_modal_snl_asm = 0 if snl_commodity!=modal_mineral_asm & snl_commodity!="none" & modal_mineral_asm!="" 

*** Modal mineral in SNL versus ASM, predicted ASM *** 
gen same_modal_snl_asm_pred = 1 if snl_commodity1==modal_asm 
replace same_modal_snl_asm_pred = 0 if modal_asm!="" & same_modal_snl_asm_pred==. & snl_commodity!="none"

*** Putting modal comparisons into matrix 
global modals "same_modal_snl_asm same_modal_snl_asm_pred"
global commodities "gold diamond 2c 3t"

matrix comparison = J(4, 10, .)

local row = 0 

foreach X in $modals {
	local column = 1
	local row = `row'+1
	
	* Percentage the same and number of cells 
	sum `X' 
	
	local mean = r(mean)
	local N = r(N)
	
	matrix comparison[`row', `column']=`mean'
	local column = `column'+1
	matrix comparison[`row', `column']=`N'
	
	
		foreach C in $commodities {
			sum `X' if snl_commodity1=="`C'"
			local mean = r(mean)
			local column = `column'+1
			matrix comparison[`row', `column']=`mean'
			local N = r(N)
			local column = `column'+1
			matrix comparison[`row', `column']=`N'
		}
	}
	

*** Present ASM mineral versus present SNL mineral

gen snl_commodity_number = 0 if snl_commodity!="none"

global allcommodities = "aluminum coal cobalt coltan copper diamond gold iron lead manganese nickel phosphate platinum silver tin tungsten zinc"

foreach C in $allcommodities {
	replace snl_`C' = 1 if snl_`C'>0 & snl_`C'!=.
	replace snl_commodity_number = snl_commodity_number + 1 if snl_`C'==1
	
}

sum snl_commodity_number 
local max = r(max)

forvalues i = 1(1)`max' {
	gen snl_mineral`i' = ""
	
}

forvalues i = 1(1)`max' {
	
	foreach C in $allcommodities {
			
		replace snl_mineral`i' = "`C'" if snl_mineral`i'=="" & snl_`C'==1 & snl_mineral1!="`C'" & snl_mineral2!="`C'" & snl_mineral3!="`C'" & snl_mineral4!="`C'" 
		
		
	}

}

* Generating indicator
forvalues i = 1(1)5 {
	
	gen same_present_snl_asm`i' = 0 if mineral1!="" & snl_mineral`i'!=""
	
	forvalues j = 1(1)5 {
		replace same_present_snl_asm`i' = 1 if mineral`j'==snl_mineral`i' & same_present_snl_asm`i'!=.
	}
}



* Results to matrix 

preserve 

reshape long same_present_snl_asm snl_mineral, i(gid) j(nomineral)

sum same_present_snl_asm 
local mean = r(mean)
local N = r(N)

local column = 1 	
matrix comparison[3, `column']=`mean'
local column = `column'+1
matrix comparison[3, `column']=`N'

replace snl_mineral = "2c" if snl_mineral=="copper" | snl_mineral=="cobalt"
replace snl_mineral = "3t" if snl_mineral=="tin" | snl_mineral=="tungsten" | snl_mineral=="coltan"

foreach C in $commodities {
	sum same_present_snl_asm if snl_mineral=="`C'"
	local mean = r(mean)
	local column = `column'+1
	matrix comparison[3, `column']=`mean'
	local N = r(N)
	local column = `column'+1
	matrix comparison[3, `column']=`N'
}
	

restore 

*** Present ASM mineral versus predicted present 

preserve 

* Generating indicator 
for var snl_mineral?: replace X = "2c" if X=="cobalt" | X=="copper" \ replace X = "3t" if X=="tin" | X=="tungsten" | X=="coltan"

forvalues i = 1(1)5 {
    
	gen same_present_snl_asm_pred`i' = 0 if snl_mineral`i'!="" & asm_voted==1
	
	foreach C in $commodities {
	    
		replace same_present_snl_asm_pred`i' = 1 if snl_mineral`i'=="`C'" & asm_voted_`C'==1
	}
	
	
}

* Results to matrix 
reshape long same_present_snl_asm_pred snl_mineral, i(gid) j(nomineral)

sum same_present_snl_asm_pred
local mean = r(mean)
local N = r(N)

local column = 1 	
matrix comparison[4, `column']=`mean'
local column = `column'+1
matrix comparison[4, `column']=`N'


foreach C in $commodities {
	sum same_present_snl_asm_pred if snl_mineral=="`C'"
	local mean = r(mean)
	local column = `column'+1
	matrix comparison[4, `column']=`mean'
	local N = r(N)
	local column = `column'+1
	matrix comparison[4, `column']=`N'
}
	

restore 



/*-----------------------------------
------- 5. Creating Table  ----------
-------------------------------------*/

clear
svmat comparison

rename comparison1 percentsame
rename comparison2 N

replace percentsame = round(percentsame*100, 0.1) 

global commodities "gold diamond comm_2c comm_3t"

local i = 3 
foreach C in $commodities {
	
	rename comparison`i' `C'
	local i = `i'+1
	rename comparison`i' `C'_n
	local i = `i'+1
	
	replace `C' = round(`C'*100, 0.1)
	label var `C' "`C' \%"

}

gen comparison = ""

replace comparison = "modal LSM = modal ASM" if _n==1
replace comparison = "modal LSM = pred. modal ASM" if _n==2
replace comparison = "pres. LSM = pres. ASM" if _n==3
replace comparison = "pres. LSM = pred. pres. ASM" if _n==4

order comparison

for var *_n N: label var X "N"
label var percentsame "\%"
label var comparison "Comparison"
label var gold "Gold \%"
label var diamond "Diam. \%"
label var comm_2c "2c \%"
label var comm_3t "3t \%"

texsave using "$Tables/Table_B3.tex", replace hlines(2) title("Comparing commodities mined by LSM and ASM") label(tab:asm_lsm_minerals) varlabels location(h) nofix align(l r r | r r | r r | r r | r r) frag
